package com.example.administrator.q1;

import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * Created by Administrator on 2017/8/29.
 */

public class ZoomOutPagetransformer implements ViewPager.PageTransformer{

    private static final float MIN_SCALE = 0.85f;
    private static final float MIN_ALPHA = 0.5f;

    @Override
    public void transformPage(View view, float postion) {

        int pageWidth = view.getWidth();
        int pageHeight = view.getHeight();

        if (postion < -1) {
            view.setAlpha(0);
        } else if (postion <= 1) {
            float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(postion));
            float vertMargin = pageHeight * (1 - scaleFactor) / 2;
            float hotzMargin = pageWidth * (1- scaleFactor) / 2;
            if (postion < 0) {
                view.setTranslationX(hotzMargin - vertMargin / 2);;
            } else {
                view.setTranslationX(0 - hotzMargin + vertMargin / 2);;
            }

            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

            // Fade the page relative to its size.
            view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE)
                    / (1 - MIN_SCALE) * (1 - MIN_ALPHA));

        } else {
            view.setAlpha(0);
        }
    }
}
